package zqkcom.atguigu.servlet;


import zqkcom.atguigu.bean.Cart;
import zqkcom.atguigu.bean.Order;
import zqkcom.atguigu.bean.OrderItem;
import zqkcom.atguigu.bean.User;
import zqkcom.atguigu.service.OrderService;
import zqkcom.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class OrderClientServlet extends BaseServlet  {

    private OrderService orderService = new OrderServiceImpl();
    /**
     * 结账
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    protected void checkOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        Cart cart = (Cart) session.getAttribute("cart");
        User user = (User) session.getAttribute("user");

        String orderId = orderService.checkOut(cart, user);

        session.removeAttribute("cart");

        session.setAttribute("orderId",orderId);

        response.sendRedirect(request.getContextPath()+"/pages/cart/checkout.jsp");

    }
    /**
     * 查询我的订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    protected void getMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");

        List<Order> list = orderService.getMyOrder(user.getId());

        request.setAttribute("list",list);

        request.getRequestDispatcher("/pages/client/order_clent.jsp").forward(request,response);

    }

    /**
     * 收货
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void takeOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");

        orderService.takeOrder(orderId);

        response.sendRedirect(request.getHeader("regerer"));

    }

    /**
     * 客户端的订单详情功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String orderId = request.getParameter("orderId");

        List<OrderItem> list = orderService.getDetails(orderId);

        request.setAttribute("list",list);

        request.getRequestDispatcher("/pages/client/order_details.jsp").forward(request,response);


    }
}
